﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.MenuCommand;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.MenuCommandHandler
{
    /// <summary>
    /// 递归显示菜单命令执行
    /// </summary>
    public class GetTreeMenuCommandHandler : IRequestHandler<GetTreeMenuCommand, APIResult<List<TreeMenuDTOs>>>
    {
        private readonly IBaseRepository<MenuModel> menuRepos;
        private readonly IBaseRepository<UserRole> userRoleRepoos;
        private readonly IBaseRepository<RoleMenuModel> roleMenuRepoos;
        private readonly IMapper mapper;

        public GetTreeMenuCommandHandler(
            IBaseRepository<MenuModel> menuRepos,
            IBaseRepository<UserRole> userRoleRepoos,
            IBaseRepository<RoleMenuModel> roleMenuRepoos,
            IMapper mapper
            )
        {
            this.menuRepos = menuRepos;
            this.userRoleRepoos = userRoleRepoos;
            this.roleMenuRepoos = roleMenuRepoos;
            this.mapper = mapper;
        }

        /// <summary>
        /// 全局用户id
        /// </summary>
        public int userId {  get; set; }
        /// <summary>
        /// 总数
        /// </summary>
        public int Count {  get; set; }

        /// <summary> 
        /// 递归显示菜单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<TreeMenuDTOs>>> Handle(GetTreeMenuCommand request, CancellationToken cancellationToken)
        {
            userId = request.UserId;
            APIResult<List<TreeMenuDTOs>> result = new APIResult<List<TreeMenuDTOs>>();
            var data = GetMenuTree(0);
            int a = data.Count;
            result.Msg = $"共查询{Count}条";
            result.data = data;
            return Task.FromResult(result);
        }
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        private List<TreeMenuDTOs> GetMenuTree(int ParentId)
        {
            var RoleList = userRoleRepoos.GetAll().Result.Where(x => x.UserId == userId);
            int[] roleId = RoleList.Select(x=>x.RoleId).ToArray();
            var menulist = roleMenuRepoos.GetAll().Result.Where(x => roleId.Contains(x.RoleId)).ToList();
           //第一种(含条件)
            var MenuData = from a in menulist
                           join b in menuRepos.GetAll().Result on a.MenuId equals b.MenuId
                           where b.ParentId == ParentId 
                           select new TreeMenuDTOs
                           {
                               Url = b.Url,
                               MenuId = b.MenuId,
                               MenuName = b.MenuName
                           };
            Count = menulist.Count();
            //第二种(不含条件)
            var treeData =  menuRepos.GetAll().Result.Where(x=>x.ParentId == ParentId);

            var model = mapper.Map<List<TreeMenuDTOs>>(MenuData);
            foreach (var item in model)
            {
                item.children = GetMenuTree(item.MenuId);
            }
            return model;
        }



    }
}
